#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygtk
import gtk

class TemplateWindow:
    def delete_event(self, widget, event, data=None):
        return False

    def destroy(self, widget, data=None):
        gtk.main_quit()
    
class MainWindow(TemplateWindow):
    def __init__(self):
        self.pages_labels = [] # L'implémentation va être à réviser. Du moins pour le moment elle fonctionne...
        self.pages_contents = []

        # Dialogs
        # Se documenter sur la bonne manière de faire des dialogues.

        self.preferences_window = PreferencesWindow()
        self.search_window = SearchWindow()
        self.build_window = BuildWindow()

        # Main window

        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Allez Cuisine !")
        self.window.set_default_size(800, 600)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)

        # Accelerators

        self.accel_group = gtk.AccelGroup()
        self.window.add_accel_group(self.accel_group)

        # Grosse vbox

        self.outer_vbox = gtk.VBox(False, 0)
        self.window.add(self.outer_vbox)

        # Menu

        self.menus = gtk.MenuBar()

        self.file_menuitem = gtk.MenuItem(label="_File")
        self.file_menu = gtk.Menu()

        self.file_new = gtk.MenuItem(label="_New")
        self.file_new.connect("activate", self.add_page)
        self.file_new.add_accelerator("activate", self.accel_group, ord("N"), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
        self.file_menu.append(self.file_new)

        self.file_import = gtk.MenuItem(label="_Import...")
        self.file_import.add_accelerator("activate", self.accel_group, ord("I"), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
        self.file_menu.append(self.file_import)

        self.file_menu.append(gtk.SeparatorMenuItem())

        self.file_quit = gtk.MenuItem(label="_Quit")
        self.file_quit.connect("activate", self.destroy)
        self.file_quit.add_accelerator("activate", self.accel_group, ord("Q"), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
        self.file_menu.append(self.file_quit)

        self.file_menuitem.set_submenu(self.file_menu)
        self.menus.add(self.file_menuitem)

        self.edit_menuitem = gtk.MenuItem(label="_Edit")
        self.edit_menu = gtk.Menu()

        self.edit_copy = gtk.MenuItem(label="_Copy")
        self.edit_copy.add_accelerator("activate", self.accel_group, ord("C"), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
        self.edit_menu.append(self.edit_copy)

        self.edit_cut = gtk.MenuItem(label="_Cut")
        self.edit_cut.add_accelerator("activate", self.accel_group, ord("X"), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
        self.edit_menu.append(self.edit_cut)

        self.edit_paste = gtk.MenuItem(label="_Paste")
        self.edit_paste.add_accelerator("activate", self.accel_group, ord("V"), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
        self.edit_menu.append(self.edit_paste)

        self.edit_menu.append(gtk.SeparatorMenuItem())

        self.edit_preferences = gtk.MenuItem(label="_Preferences...")
        self.edit_preferences.connect("activate", self.show_preferences, None)
        self.edit_menu.add(self.edit_preferences)

        self.edit_menuitem.set_submenu(self.edit_menu)
        self.menus.add(self.edit_menuitem)

        self.tools_menuitem = gtk.MenuItem(label="_Tools")
        self.tools_menu = gtk.Menu()

        self.tools_menuitem.set_submenu(self.tools_menu)
        self.menus.add(self.tools_menuitem)

        self.help_menuitem = gtk.MenuItem(label="_Help")
        self.help_menu = gtk.Menu()

        self.help_help = gtk.MenuItem(label="_Online help")
        self.help_menu.append(self.help_help)

        self.help_about = gtk.MenuItem(label="_About")
        self.help_about.connect("activate", self.show_about, None)
        self.help_menu.append(self.help_about)

        self.help_menuitem.set_submenu(self.help_menu)
        self.menus.add(self.help_menuitem)
        
        self.outer_vbox.pack_start(self.menus, False, 0)

        # Toolbar
        self.toolbar = gtk.Toolbar()
        self.toolbar.set_style(gtk.TOOLBAR_ICONS)
        
        self.toolbar_new = gtk.ToolButton(gtk.STOCK_NEW)
        self.toolbar_new.connect("clicked", self.add_page, None)
        self.toolbar.add(self.toolbar_new)

        self.toolbar.add(gtk.SeparatorToolItem())

        self.toolbar_copy = gtk.ToolButton(gtk.STOCK_COPY)
        self.toolbar.add(self.toolbar_copy)

        self.toolbar_cut = gtk.ToolButton(gtk.STOCK_CUT)
        self.toolbar.add(self.toolbar_cut)

        self.toolbar_paste = gtk.ToolButton(gtk.STOCK_PASTE)
        self.toolbar.add(self.toolbar_paste)

        self.toolbar.add(gtk.SeparatorToolItem())

        self.toolbar_preferences = gtk.ToolButton(gtk.STOCK_PREFERENCES)
        self.toolbar_preferences.connect("clicked", self.show_preferences, None)
        self.toolbar.add(self.toolbar_preferences)

        self.toolbar_searchspace = gtk.SeparatorToolItem()
        self.toolbar_searchspace.set_draw(False)
        self.toolbar_searchspace.set_expand(True)
        self.toolbar.add(self.toolbar_searchspace)

        self.toolbar_searchentry = gtk.ToolItem()
        self.toolbar_searchentry.add(gtk.Entry())
        self.toolbar.add(self.toolbar_searchentry)

        self.toolbar_searchbutton = gtk.ToolButton(gtk.STOCK_FIND)
        self.toolbar.add(self.toolbar_searchbutton)

        self.outer_vbox.pack_start(self.toolbar, False, 0)

        # Notebook

        self.notes = gtk.Notebook()

        self.outer_vbox.pack_start(self.notes, False, 0)

        # About dialog

        self.about_dialog = gtk.AboutDialog()
        self.about_dialog.set_name("Allez Cuisine!")
        self.about_dialog.set_version("0.1a")
        self.about_dialog.set_website("http://code.google.com/p/allezcuisine")
        self.about_dialog.set_authors(["Alexandre Boily", "Thomas T. M."])

        # Show

        self.window.show_all()

    def main(self):
        gtk.main()

    def add_page(self, widget, event, data=None):
        self.pages_labels.append(gtk.HBox())
        self.pages_labels[-1].pack_start(gtk.Label(str="Random Label"))
        self.pages_labels[-1].pack_start(gtk.ToolButton(gtk.STOCK_CLOSE))
        self.pages_labels[-1].show_all()
        self.pages_contents += [gtk.VBox()]
        self.pages_contents[-1].add(gtk.Label(str="The Incredible Surrealist Content of the Tabulary Tab"))
        self.notes.append_page(self.pages_contents[-1], self.pages_labels[-1])
        self.window.show_all()

    def remove_page(self, widget, event, data=None):
        pass

    def show_about(self, widget, event, data=None):
        self.about_dialog.show()

    def show_preferences(self, widget, event, data=None):
        self.preferences_window.window.show_all()

class PreferencesWindow(TemplateWindow):
    def __init__(self):

        # Preferences window
        
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_modal(True)
        self.window.set_title("Preferences")
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)

        # Outer VBox

        self.outer_vbox = gtk.VBox()
        self.window.add(self.outer_vbox)

        # Preferences

        self.dummy_label = gtk.Label(str="Dummy label")
        self.outer_vbox.pack_start(self.dummy_label, False, 0)

        # Validation buttons

        self.outer_vbox.add(gtk.HSeparator())

        self.buttons_hbox = gtk.HBox(False, 5) # Remanier le padding.
        
        self.ok_button = gtk.Button(stock=gtk.STOCK_OK)
        self.ok_button.connect("clicked", self.ok_cb, None)
        self.buttons_hbox.pack_end(self.ok_button, False, False, 0)

        self.cancel_button = gtk.Button(stock=gtk.STOCK_CANCEL)
        self.cancel_button.connect("clicked", self.cancel_cb, None)
        self.buttons_hbox.pack_end(self.cancel_button, False, False, 0)

        self.outer_vbox.pack_start(self.buttons_hbox, False, 0)

    def cancel_cb(self, widget, event, data=None):
        pass

    def ok_cb(self, widget, event, data=None):
        pass

class SearchWindow(TemplateWindow):
    pass

class BuildWindow(TemplateWindow):
    pass

if __name__ == "__main__":
    main_window = MainWindow()
    main_window.main()
